/**
 * Project Name:fastPlat
 * File Name:MemCompanyController.java
 * Package Name:com.fast.user.web.controller.member
 * Date:2017年05月21日 21时54分44秒
 * Copyright (c) 2017, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.controller.member;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.common.userCache.UserRequestUtils;
import com.fast.user.model.member.MemCompany;
import com.fast.user.model.member.Member;
import com.fast.user.service.member.MemCompanyService;
import com.fast.user.web.form.web.MemCompanyForm;

/**
 * ClassName:MemCompanyController <br/>
 * 用途: MemCompany web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2017年05月21日 21时54分44秒 <br/> 	 
 */
@Controller
@RequestMapping("/memCompany")
public class MemCompanyController extends BaseController{
	
	@Resource(name="memCompanyService")
	private MemCompanyService memCompanyService;
		
	/**
	 * 得到部门下拉列表
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMemCompanyList4comboBox.do")
	public List<MemCompany> getMemCompanyList4comboBox(String parentUUID){
		List<MemCompany> dicList =  this.memCompanyService.queryForBeanList(MemCompany.class, " where 1=1");
	    return dicList;
	}

	/**
	 * 编辑[MemCompanyService]信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editMemCompany.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑企业信息信息")
	public ResponseData editMemCompanyService(){
		ResponseData responseData = new ResponseData();
		MemCompany memCompany = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				memCompany = this.memCompanyService.getById(MemCompany.class,id);
				memCompany = (MemCompany) webUtil.bindDataToBean(memCompany);
			}else{
				memCompany = new MemCompany();
				memCompany = (MemCompany) webUtil.bindDataToBean(memCompany);
				UserRequestUtils.bindCreaterInfo(memCompany);
			}
			UserRequestUtils.bindEditorInfo(memCompany);
			Member member = this.memCompanyService.queryForBeanByUUID(Member.class, memCompany.getMemberUUID());
			memCompany.setLoginName(member.getNickName());
			boolean actionFlag = this.memCompanyService.saveOrModify(memCompany, "where memberUUID='"+memCompany.getMemberUUID()+"'");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除[MemCompany]信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemCompanyByIdForHS.do")
	@AnoSysLog(actionName="删除企业信息到回收站")
	public ResponseData delMemCompanyByIdForHS(int id) {
		LogUtils.info("delMemCompanyByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(MemCompany.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = memCompanyService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemCompanyByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除[MemCompany]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemCompanyById.do")
	@AnoSysLog(actionName="彻底删除某个企业信息")
	public ResponseData delMemCompanyById(int id) {
		LogUtils.info("delMemCompanyById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = memCompanyService.deleteById(MemCompany.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemCompanyById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个[MemCompany]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelMemCompany.do")
	@AnoSysLog(actionName="批量删除文章评论")
	public ResponseData batchDelMemCompany(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = memCompanyService.deleteByIds(MemCompany.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 得到[MemCompany]列表
	 * @return PageView<MemCompany>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/getMemCompanyList.do")
	public Object getMemCompanyList(MemCompanyForm memCompanyForm){
		LogUtils.info("getMemCompanyList param:"+memCompanyForm.toString());
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<MemCompany> pageView = null;
		try {
			pageView = this.memCompanyService.getMemCompanyList(memCompanyForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			LogUtils.error("getMemCompanyList err:",e);
		}
		LogUtils.info("getMemCompanyList result:"+pageView.toString());
 		return gridMap;
	}
	
	/**
	 * 得到[Member]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMemCompanyDetail.do")
	public ResponseData getMemCompanyDetail(String memberUUID) {
		ResponseData responseData = new ResponseData();
		MemCompany memCompany = memCompanyService.queryForBean(MemCompany.class, " where memberUUID='"+memberUUID+"'");
		try {
			responseData.setData(memCompany);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
}

